package com.zjsru.plan2025.oneday;

import java.util.Arrays;

/**
 * 506. 相对名次
 *
 * @Author: cookLee
 * @Date: 2025-04-10
 */
public class FindRelativeRanks {

    /**
     * 主
     * \
     * 输入：score = [5,4,3,2,1]
     * 输出：["Gold Medal","Silver Medal","Bronze Medal","4","5"]
     * 解释：名次为 [1st, 2nd, 3rd, 4th, 5th] 。
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        FindRelativeRanks findRelativeRanks = new FindRelativeRanks();
        int[] score = new int[]{5, 4, 3, 2, 1};
        System.out.println(Arrays.toString(findRelativeRanks.findRelativeRanks(score)));
    }

    public String[] findRelativeRanks(int[] score) {
        if (score == null || score.length == 0) {
            return new String[0];
        }
        final String GOLD = "Gold Medal";
        final String SILVER = "Silver Medal";
        final String BRONZE = "Bronze Medal";

        int n = score.length;
        String[] ans = new String[n];

        int[][] indexedScore = new int[n][2];
        for (int i = 0; i < n; i++) {
            // 分数
            indexedScore[i][0] = score[i];
            // 原始索引
            indexedScore[i][1] = i;
        }
        // 按分数降序排序
        Arrays.sort(indexedScore, (a, b) -> b[0] - a[0]);
        // 分配排名
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                ans[indexedScore[i][1]] = GOLD;
            } else if (i == 1) {
                ans[indexedScore[i][1]] = SILVER;
            } else if (i == 2) {
                ans[indexedScore[i][1]] = BRONZE;
            } else {
                ans[indexedScore[i][1]] = String.valueOf(i + 1);
            }
        }

        return ans;
    }

}
